import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';
import { Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const objectOrError: any = exception.getResponse();
    const resObj: any =
      typeof objectOrError == 'object'
        ? objectOrError
        : { message: objectOrError };
    response.status(200).json({
      ...resObj,
      result: null,
      success: false,
      timestamp: Date.now(),
    });
  }
}
